import 'package:flutter/material.dart';
import '../../core/app_export.dart';

/// Helper class for managing themes and colors.
class ThemeHelper {
  // The current app theme
  final String _appTheme = PrefUtils.getThemeData(); // Assuming it returns a string like 'primary' or 'dark'

  // A map of custom color themes supported by the app
  final Map<String, PrimaryColors> _supportedCustomColor = {
    'primary': PrimaryColors.light(),
    'dark': PrimaryColors.dark(), // Assuming you create a method for dark theme colors
  };

  /// Returns the primary colors for the current theme.
  PrimaryColors _getThemeColors() {
    //throw exception to notify given theme is not found or not generated by the generator
    if (!_supportedCustomColor.containsKey(_appTheme)) {
      throw Exception(
          "$_appTheme is not found.Make sure you have added this theme class in JSON Try running flutter pub run build_runner");
    }
    //return theme from map

    return _supportedCustomColor[_appTheme] ?? PrimaryColors.dark();
  }

  /// Returns the current theme data.
  ThemeData _getThemeData() {
    if (_appTheme == 'dark') {
      return _getThemeDarkData();
    }

    var colorScheme = ColorSchemes.primaryColorScheme;
    return ThemeData(
      fontFamily: 'Tajawal',
      visualDensity: VisualDensity.standard,
      colorScheme: colorScheme,
      textTheme: TextThemes.textTheme(),
      primaryTextTheme: TextTheme(
        bodySmall: TextStyle(
          color: Colors.black,
          fontSize: 14.fSize,
        ),
      ),
      scaffoldBackgroundColor: colorScheme.onPrimaryContainer.withOpacity(1),
      appBarTheme: const AppBarTheme(
        iconTheme: IconThemeData(
          color: Colors.black,
        ),
      ),
      elevatedButtonTheme: ElevatedButtonThemeData(
        style: ElevatedButton.styleFrom(
          backgroundColor: colorScheme.primary.withOpacity(1),
          shape: RoundedRectangleBorder(
            borderRadius: BorderRadius.circular(5.h),
          ),
          shadowColor: colorScheme.primary,
          elevation: 10,
          visualDensity: const VisualDensity(
            vertical: -4,
            horizontal: -4,
          ),
          padding: EdgeInsets.zero,
        ),
      ),
      outlinedButtonTheme: OutlinedButtonThemeData(
        style: OutlinedButton.styleFrom(
          backgroundColor: Colors.transparent,
          side: BorderSide(
            color: Colors.deepOrange,
            width: 1.h,
          ),
          shape: RoundedRectangleBorder(
            borderRadius: BorderRadius.circular(5.h),
          ),
          visualDensity: const VisualDensity(
            vertical: -4,
            horizontal: -4,
          ),
          padding: EdgeInsets.zero,
        ),
      ),
      inputDecorationTheme: InputDecorationTheme(
        hintStyle: TextStyle(
          color: Colors.black54,
          fontSize: 14.fSize,
        ),
        fillColor: ColorSchemes.primaryColorScheme.onPrimaryContainer,
        border: const OutlineInputBorder(
          borderSide: BorderSide(
            color: Colors.black,
          ),
        ),
        enabledBorder: const OutlineInputBorder(
          borderSide: BorderSide(
            color: Colors.black,
            width: 1,
          ),
        ),
        focusedBorder: const OutlineInputBorder(
          borderSide: BorderSide(
            color: Colors.deepOrange,
            width: 1,
          ),
        ),
      ),
      dividerTheme: const DividerThemeData(
        thickness: 1,
        space: 1,
        color: Colors.black,
      ),
    );
  }

  /// Returns the current theme data for dark mode.
  ThemeData _getThemeDarkData() {
    var colorScheme = ColorSchemes.primaryColorDarkScheme;
    return ThemeData(
      fontFamily: 'Tajawal',
      visualDensity: VisualDensity.standard,
      colorScheme: colorScheme,
      textTheme: TextThemes.textTheme(),
      primaryTextTheme: TextTheme(
        bodySmall: TextStyle(
          color: Colors.white,
          fontSize: 14.fSize,
        ),
      ),
      scaffoldBackgroundColor: colorScheme.onPrimaryContainer.withOpacity(1),
      appBarTheme: const AppBarTheme(
        iconTheme: IconThemeData(
          color: Colors.white,
        ),
      ),
      elevatedButtonTheme: ElevatedButtonThemeData(
        style: ElevatedButton.styleFrom(
          backgroundColor: colorScheme.primary.withOpacity(1),
          shape: RoundedRectangleBorder(
            borderRadius: BorderRadius.circular(5.h),
          ),
          shadowColor: colorScheme.primary,
          elevation: 10,
          visualDensity: const VisualDensity(
            vertical: -4,
            horizontal: -4,
          ),
          padding: EdgeInsets.zero,
        ),
      ),
      outlinedButtonTheme: OutlinedButtonThemeData(
        style: OutlinedButton.styleFrom(
          backgroundColor: Colors.transparent,
          side: BorderSide(
            color: Colors.deepOrange,
            width: 1.h,
          ),
          shape: RoundedRectangleBorder(
            borderRadius: BorderRadius.circular(5.h),
          ),
          visualDensity: const VisualDensity(
            vertical: -4,
            horizontal: -4,
          ),
          padding: EdgeInsets.zero,
        ),
      ),
      inputDecorationTheme: InputDecorationTheme(
        hintStyle: TextStyle(
          color: Colors.white54,
          fontSize: 14.fSize,
        ),
        fillColor: ColorSchemes.primaryColorDarkScheme.onPrimaryContainer,
        border: const OutlineInputBorder(
          borderSide: BorderSide(
            color: Colors.white,
          ),
        ),
        enabledBorder: const OutlineInputBorder(
          borderSide: BorderSide(
            color: Colors.white,
            width: 1,
          ),
        ),
        focusedBorder: const OutlineInputBorder(
          borderSide: BorderSide(
            color: Colors.deepOrange,
            width: 1,
          ),
        ),
      ),
      dividerTheme: const DividerThemeData(
        thickness: 1,
        space: 1,
        color: Colors.deepOrange,
      ),
    );
  }

  /// Returns the primary colors for the current theme.
  PrimaryColors themeColor() => _getThemeColors();

  /// Returns the current theme data.
  ThemeData themeData() => _getThemeData();
  //ThemeData themeDarkData() => _getThemeDarkData();
}

/// Class containing the supported text theme styles.
class TextThemes {
  static TextTheme textTheme() => TextTheme(
    bodySmall: TextStyle(
      color: appTheme.labelSmallColor,
      fontSize: 12.fSize,
      fontWeight: FontWeight.w400,
    ),
    headlineSmall: TextStyle(
      color: appTheme.labelSmallColor,
      fontSize: 24.fSize,
      fontWeight: FontWeight.w700,
    ),
    labelLarge: TextStyle(
      color: appTheme.labelLargeColor,
      fontSize: 12.fSize,
      fontFamily: 'Tajawal',
      fontWeight: FontWeight.w700,
    ),
    labelMedium: TextStyle(
      color: appTheme.labelMediumColor,
      fontSize: 10.fSize,
      fontWeight: FontWeight.w700,
    ),
    labelSmall: TextStyle(
      color: appTheme.labelSmallColor,
      fontSize: 10.fSize,
      fontWeight: FontWeight.w700,
    ),
    titleLarge: TextStyle(
      color: appTheme.titleLargeColor,
      fontSize: 20.fSize,
      fontWeight: FontWeight.w700,
    ),
    titleMedium: TextStyle(
      color: appTheme.titleMediumColor,
      fontSize: 18.fSize,
      fontWeight: FontWeight.bold,
    ),
    titleSmall: TextStyle(
      color: appTheme.titleSmallColor,
      fontSize: 16.fSize,
      fontWeight: FontWeight.w700,
    ),
  );
  static TextTheme textDarkTheme() => TextTheme(
    bodySmall: TextStyle(
      color: appTheme.labelSmallColor,
      fontSize: 12.fSize,
      fontWeight: FontWeight.w400,
    ),
    headlineSmall: TextStyle(
      color: appTheme.labelSmallColor,
      fontSize: 24.fSize,
      fontWeight: FontWeight.w700,
    ),
    labelLarge: TextStyle(
      color: appTheme.labelLargeColor,
      fontSize: 12.fSize,
      fontFamily: 'Tajawal',
      fontWeight: FontWeight.w700,
    ),
    labelMedium: TextStyle(
      color: appTheme.labelMediumColor,
      fontSize: 10.fSize,
      fontWeight: FontWeight.w700,
    ),
    labelSmall: TextStyle(
      color: appTheme.labelSmallColor,
      fontSize: 10.fSize,
      fontWeight: FontWeight.w700,
    ),
    titleLarge: TextStyle(
      color: appTheme.titleLargeColor,
      fontSize: 20.fSize,
      fontWeight: FontWeight.w700,
    ),
    titleMedium: TextStyle(
      color: appTheme.titleMediumColor,
      fontSize: 18.fSize,
      fontWeight: FontWeight.bold,
    ),
    titleSmall: TextStyle(
      color: appTheme.titleSmallColor,
      fontSize: 16.fSize,
      fontWeight: FontWeight.w700,
    ),
  );
}

/// Class containing the supported color schemes.
class ColorSchemes {
  static const primaryColorScheme = ColorScheme.light(
    // Primary colors
    primary: Color(0xFF83673D),
    // On colors (text colors)
    onPrimary: Color(0xFFFF9800),
    onPrimaryContainer: Color(0xFFFCFBFB),
    secondary: Colors.white,
  );

  static const primaryColorDarkScheme = ColorScheme.dark(
    // Primary colors
    primary: Color(0xFF83673D),
    // On colors (text colors)
    onPrimary: Color(0xFFFF9800),
    secondary: Color(0xff3a3737),
    onPrimaryContainer: Color(0xFF000000),
  );
}

/// Class containing custom colors for a primary theme.
class PrimaryColors {
  late Color  labelLargeColor ;
  late Color  labelSmallColor ;
  late Color  labelMediumColor;
  late Color  titleLargeColor ;
  late Color  titleMediumColor ;
  late Color  titleSmallColor ;

  // Constructor for dark theme colors
  PrimaryColors.dark() {
  // Custom dark theme colors initialization here, if needed
    labelLargeColor  = Colors.white;
    labelSmallColor  = Colors.white;
    labelMediumColor  = Colors.white;
    titleLargeColor  = Colors.white;
    titleMediumColor  = Colors.white;
    titleSmallColor  = Colors.white;
  }
  // Constructor for dark theme colors
  PrimaryColors.light() {
  // Custom light theme colors initialization here, if needed
    labelLargeColor  = Colors.black54;
    labelSmallColor  = Colors.black54;
    labelMediumColor  = Colors.black54;
    titleLargeColor  = Colors.black54;
    titleMediumColor  = Colors.black54;
    titleSmallColor  = Colors.black54;
  }
}

PrimaryColors get appTheme => ThemeHelper().themeColor();

ThemeData get theme => ThemeHelper().themeData();
ThemeData get themeDark => ThemeHelper().themeData();
